<?php

namespace App\Providers;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    protected $namespace = 'App\Service';

    /**
     * Bootstrap any application services.
     */
    public function boot()
    {
        if ('local' === env('APP_ENV')) {
            DB::connection()->enableQueryLog();
            Event::listen('kernel.handled', function ($request, $response) {
                if ($request->has('sql-debug')) {
                    $queries = DB::getQueryLog();
                    if (!empty($queries)) {
                        foreach ($queries as &$query) {
                            $query['full_query'] = vsprintf(str_replace('?', '%s', $query['query']), $query['bindings']);
                        }
                    }
                    dd($queries);
                }
            });
        }
    }

    /**
     * Register any application services.
     */
    public function register()
    {
    }
}
